package edu.tfsu.team_2208.skylarkim.service;

import ch.qos.logback.core.joran.event.SaxEventRecorder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.tfsu.team_2208.skylarkim.mapper.WordxuMapper;
import edu.tfsu.team_2208.skylarkim.mapper.entity.Wordxu;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import lombok.extern.java.Log;
import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.yarn.webapp.hamlet.HamletSpec;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import static java.nio.file.Files.exists;
@Log
@Service
public class HdfsService {

    @Value("${Xu.hdfs.path}")
    private String path;

    @Value("skylarkim")
    private String username;

    @Resource
    private WordxuMapper wordxuMapper;

    private static String hdfsPath;
    private static String hdfsName;

    private static final int bufferSize = 1024 * 1024 * 64;

    /**
     * 获取HDFS配置
     * @return
     */
    private static Configuration getConfiguration() {
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS", hdfsPath);
        return conf;
    }

    /**
     * 获取HDFS文件系统
     * @return
     */
    public static FileSystem getFileSystem() {
        try {
            FileSystem fs = FileSystem.get(new URI(hdfsPath), getConfiguration(), hdfsName);
            return fs;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 判断文件是否存在
     * @param path
     * @return
     */
    public static boolean existFile(String path) {
        if (StringUtils.isEmpty(path)) {
            return false;
        }

        try {
            FileSystem fs = getFileSystem();
            Path srcPath = new Path(path);
            if (fs.exists(srcPath)) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }



    /**
     * 读取文件
     * @param path
     * @return
     */
    public List<String> readFile(String path,int start,int limit) {
        if (!existFile(path)) {
            return null;
        }

        List<String> lines = new ArrayList<>();
        FileSystem fs = getFileSystem();
        Path srcPath = new Path(path);
        BufferedReader reader;
        try {
            reader = new BufferedReader(new InputStreamReader(fs.open(srcPath)));
            String line;
            int count = 0;
            while ((line = reader.readLine()) != null && (lines.size() < limit || limit == -1)) {
                count++;
                if (count <= start) {
                    continue;
                }
                String newLine = line.replace("\t", ",")
                        .replace("\"", "")
                        .replace("\\", "");

                String[] parts = newLine.split(",");
                if (parts.length == 2 && !parts[0].isEmpty() && !parts[1].isEmpty()) {
                    try {
                        int countValue = Integer.parseInt(parts[1]);
                        Wordxu wordxu = new Wordxu();
                        wordxu.setWord(parts[0]);
                        wordxu.setCount(countValue);
                        wordxuMapper.insert(wordxu);
                    } catch (NumberFormatException e) {
                        log.warning("Invalid count format: " + parts[1]);
                    }
                } else {
                    log.warning("Invalid line format: " + line);
                }










            }
            reader.close();
            fs.close();
            return lines;
        } catch (IOException e) {
            e.printStackTrace();
            log.warning( "Hadoop文件读取错误");
        }

        return null;
    }


    public List<Wordxu> selectFromWordxu(int limit) {
        if (limit==-1) {
            return wordxuMapper.selectList(null);
        }
        QueryWrapper<Wordxu> wrapper = new QueryWrapper<Wordxu>();
        wrapper.orderByDesc("count");
        IPage<Wordxu> page =wordxuMapper.selectPage(new Page<Wordxu>(1,limit),wrapper);
        return page.getRecords();
    }


    /**
     * 获取HDFS路径
     */
    @PostConstruct
    public void getPath() {
        hdfsPath = "hdfs://192.168.136.129:8020";
    }

    /**
     * 获取HDFS用户名
     */
    @PostConstruct
    public void getName() {
        hdfsName = "skylarkim";
    }

    public static String getHadoopPath() {
        return hdfsPath;
    }

    public static String getHadoopName() {
        return hdfsName;
    }



}







